
Page({
  data: {
    date: '', // 当前日期
    goal: 2000, // 默认饮水目标
    goalRange: [1500, 2000, 2500, 3000], // 饮水目标选项
    currentWater: 0, // 当前饮水量
    additionalWater:200,//每次喝水量
    additionalWaterRange:[100,150,200,250,300],//喝水量选项
    records: [], // 饮水记录
    currentWaterPercentStr:0 + '%'
  },
 
  // 页面加载时设置初始日期
  onLoad() {
    const today = this.getFormattedDate(new Date());
    this.setData({ date: today });
  },
 
  // 获取格式化的日期
  getFormattedDate(date) {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    return `${year}-${month}-${day}`;
  },

  // 切换到前一天
  prevDay() {
    const currentDate = new Date(this.data.date);
    currentDate.setDate(currentDate.getDate() - 1);
    this.setData({ date: this.getFormattedDate(currentDate) });
  },

  // 切换到后一天
  nextDay() {
    const currentDate = new Date(this.data.date);
    currentDate.setDate(currentDate.getDate() + 1);
    this.setData({ date: this.getFormattedDate(currentDate) });
  },

  // 设置饮水目标
  setGoal(e) {
    const selectedGoal = this.data.goalRange[e.detail.value];
    this.setData({ 
      goal: selectedGoal ,
      currentWaterPercentStr: ((this.data.currentWater / this.data.goal) * 100) + '%'
    });
  },
  //设置单次饮水量
  setGoalWater(e){
    const selectedWaterGoal = this.data.additionalWaterRange[e.detail.value];
    this.setData({ additionalWater: selectedWaterGoal });
  },
  // 记录饮水
  addWater() {
    // const additionalWater = 200; // 每次增加 200ml
    // const newWater = this.data.currentWater + additionalWater;
    //console.log(this.data.currentWater);
    //console.log(this.data.additionalWater);
    const newWater = this.data.currentWater + this.data.additionalWater;
    // 更新当前水量，记录饮水量
    if (newWater <= this.data.goal) {
      this.setData({
        currentWater: newWater,
        currentWaterPercentStr: ((newWater / this.data.goal) * 100) + '%',
        records: [
          ...this.data.records,
          { amount: this.data.additionalWater, time: this.getFormattedTime() }
        ]
      });
    }
  },

  // 获取当前时间
  getFormattedTime() {
    const now = new Date();
    const hours = now.getHours().toString().padStart(2, '0');
    const minutes = now.getMinutes().toString().padStart(2, '0');
    return `${hours}:${minutes}`;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})